using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using DAO;
using System.Data;
using System.Xml;
using Newtonsoft.Json.Linq;

namespace BUS
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class ChuyenSanXuatControl : BUSControl
    {
        #region Local Variable
        private ChuyenSanXuatDataAccess _objDAO;
        #endregion Local Variable

        public ChuyenSanXuatControl(string type, string connectString, int timeout = 0)
            : base(type, connectString, timeout) { _objDAO = new ChuyenSanXuatDataAccess(Connnection); }

        public ChuyenSanXuatControl(BUSControl common)
            : base(common) { _objDAO = new ChuyenSanXuatDataAccess(common.Connnection); }

        #region Method
        public DataTable GetShortData(String XuongId, List<DTO.FilterParameter> filters, ref string sErr)
        {
            return _objDAO.GetShortData(XuongId, filters, ref sErr);
        }

        public DataTable GetByFilterToDataTable(String XuongId, List<DTO.FilterParameter> filters, ref string logMsg, int indexPage = 0, int itemPerPage = 0)
        {
            return _objDAO.GetByFilterToDataTable(XuongId, filters, ref logMsg, indexPage, itemPerPage);
        }

        public int GetCountRecord(String XuongId, List<DTO.FilterParameter> filters, ref string sErr)
        {
            return _objDAO.GetCountRecord(XuongId, filters, ref sErr);
        }

        public ChuyenSanXuatInfo[] GetByFilter(String XuongId, List<DTO.FilterParameter> filters, ref string logMsg, int indexPage = 0, int itemPerPage = 0)
        {
            ChuyenSanXuatInfo[] res = _objDAO.GetByFilter(XuongId, filters, ref logMsg, indexPage, itemPerPage);
            //get details
            BUS.ChuyenLCDControl ctr = new ChuyenLCDControl(this);
            if (string.IsNullOrEmpty(logMsg) && res != null)
            {
                foreach (var x in res)
                {
                    if (!string.IsNullOrEmpty(logMsg)) break;
                    x.Lcds = ctr.GetChuyenLCD(XuongId, x.LineId.Value, ref logMsg);                    
                }
            }
            return res;
        }



        public int Add(ChuyenSanXuatInfo obj, ref string sErr)
        {
            _objDAO.Add(obj, ref sErr);
            if (sErr == "")
            {
                //add details
                BUS.ChuyenLCDControl ctr = new ChuyenLCDControl(this);
                if (obj.Lcds != null)
                {
                    foreach (var x in obj.Lcds)
                    {
                        ctr.Add(x, ref sErr);
                    }
                }
            }
            return 1;
        }

        public string Update(ChuyenSanXuatInfo obj)
        {
            string sErr = _objDAO.Update(obj);
            // delete details
            if (string.IsNullOrEmpty(sErr))
            {
                BUS.ChuyenLCDControl ctr = new ChuyenLCDControl(this);

                sErr = ctr.Delete(obj.XuongId, obj.LineId.Value);
                if (string.IsNullOrEmpty(sErr))
                {
                    if (obj.Lcds != null)
                    {
                        foreach (var x in obj.Lcds)
                        {
                            if (!string.IsNullOrEmpty(sErr)) break;
                            ctr.Add(x, ref sErr);
                        }
                    }
                }
            }
            // add details
            return sErr;
        }

        public string Delete(String XuongId, Int32 LineId)
        {
            string sErr = "";
            // delete details

            if (sErr == "") sErr = _objDAO.Delete(XuongId, LineId);
            return sErr;
        }
        public bool IsExist(String XuongId, Int32? LineId, ref string sErr)
        {
            return _objDAO.IsExist(XuongId, LineId, ref sErr);
        }

        public string InsertUpdate(ChuyenSanXuatInfo obj)
        {
            string sErr = "";
            if (IsExist(obj.XuongId, obj.LineId, ref sErr)) { sErr = Update(obj); }
            else
            {
                if (sErr == "") Add(obj, ref sErr);
            }
            return sErr;
        }

        public string TransferIn(DataRow row)
        {
            ChuyenSanXuatInfo inf = new ChuyenSanXuatInfo(row);
            return InsertUpdate(inf);
        }

        public override string ToXmlString(DTOInfo[] lInf, ref string sErr)
        {
            string result = "";
            try
            {
                if (lInf != null && lInf.Length > 0)
                {
                    XmlDocument doc = new XmlDocument();
                    XmlElement root = doc.CreateElement("ChuyenSanXuat");
                    doc.AppendChild(root);
                    foreach (DTO.DTOInfo x in lInf)
                    {
                        DTO.ChuyenSanXuatInfo it = x as DTO.ChuyenSanXuatInfo;
                        if (it == null) return "";

                        XmlElement row = doc.CreateElement("row");

                        if (it.LineId != null) row.SetAttribute("LineId", string.Format("{0}", it.LineId));
                        if (it.LoaiBang != null) row.SetAttribute("LoaiBang", string.Format("{0}", it.LoaiBang));
                        if (it.TenChuyen != null) row.SetAttribute("TenChuyen", string.Format("{0}", it.TenChuyen));
                        if (it.KhachHang != null) row.SetAttribute("KhachHang", string.Format("{0}", it.KhachHang));
                        if (it.MaHang != null) row.SetAttribute("MaHang", string.Format("{0}", it.MaHang));
                        if (it.SoLuongMaHang != null) row.SetAttribute("SoLuongMaHang", string.Format("{0}", it.SoLuongMaHang));
                        if (it.DonGia != null) row.SetAttribute("DonGia", string.Format("{0}", it.DonGia));
                        if (it.SoLaoDong != null) row.SetAttribute("SoLaoDong", string.Format("{0}", it.SoLaoDong));
                        if (it.SoVang != null) row.SetAttribute("SoVang", string.Format("{0}", it.SoVang));
                        if (it.TyLeDat != null) row.SetAttribute("TyLeDat", string.Format("{0}", it.TyLeDat));
                        if (it.SoGiaiDoan != null) row.SetAttribute("SoGiaiDoan", string.Format("{0}", it.SoGiaiDoan));
                        if (it.DinhMuc1 != null) row.SetAttribute("DinhMuc1", string.Format("{0}", it.DinhMuc1));
                        if (it.DinhMuc2 != null) row.SetAttribute("DinhMuc2", string.Format("{0}", it.DinhMuc2));
                        if (it.DinhMuc3 != null) row.SetAttribute("DinhMuc3", string.Format("{0}", it.DinhMuc3));
                        if (it.DinhMuc4 != null) row.SetAttribute("DinhMuc4", string.Format("{0}", it.DinhMuc4));
                        if (it.DinhMuc5 != null) row.SetAttribute("DinhMuc5", string.Format("{0}", it.DinhMuc5));
                        if (it.HoanTat1 != null) row.SetAttribute("HoanTat1", string.Format("{0}", it.HoanTat1));
                        if (it.HoanTat2 != null) row.SetAttribute("HoanTat2", string.Format("{0}", it.HoanTat2));
                        if (it.HoanTat3 != null) row.SetAttribute("HoanTat3", string.Format("{0}", it.HoanTat3));
                        if (it.HoanTat4 != null) row.SetAttribute("HoanTat4", string.Format("{0}", it.HoanTat4));
                        if (it.HoanTat5 != null) row.SetAttribute("HoanTat5", string.Format("{0}", it.HoanTat5));
                        if (it.LuyKeSL1 != null) row.SetAttribute("LuyKeSL1", string.Format("{0}", it.LuyKeSL1));
                        if (it.LuyKeSL2 != null) row.SetAttribute("LuyKeSL2", string.Format("{0}", it.LuyKeSL2));
                        if (it.LuyKeSL3 != null) row.SetAttribute("LuyKeSL3", string.Format("{0}", it.LuyKeSL3));
                        if (it.LuyKeSL4 != null) row.SetAttribute("LuyKeSL4", string.Format("{0}", it.LuyKeSL4));
                        if (it.LuyKeSL5 != null) row.SetAttribute("LuyKeSL5", string.Format("{0}", it.LuyKeSL5));
                        if (it.KeHoachSL1 != null) row.SetAttribute("KeHoachSL1", string.Format("{0}", it.KeHoachSL1));
                        if (it.KeHoachSL2 != null) row.SetAttribute("KeHoachSL2", string.Format("{0}", it.KeHoachSL2));
                        if (it.KeHoachSL3 != null) row.SetAttribute("KeHoachSL3", string.Format("{0}", it.KeHoachSL3));
                        if (it.KeHoachSL4 != null) row.SetAttribute("KeHoachSL4", string.Format("{0}", it.KeHoachSL4));
                        if (it.Comments != null) row.SetAttribute("Comments", string.Format("{0}", it.Comments));
                        if (it.WorkTimeOfDay != null) row.SetAttribute("WorkTimeOfDay", string.Format("{0}", it.WorkTimeOfDay));
                        if (it.WorkTimeOfNow != null) row.SetAttribute("WorkTimeOfNow", string.Format("{0}", it.WorkTimeOfNow));
                        // details
                        //if (it.details != null) row.SetAttribute("details", JsonConvert.SerializeObject(it.details));

                        root.AppendChild(row);
                    }

                    result = doc.InnerXml;
                }
            }
            catch (Exception ex) { sErr = ex.Message; }
            return result;
        }

        public override string TransferIn(DTOInfo inf, BUS.BUSControl.TransferMode mode)
        {
            string sErr = "";
            if (inf is ChuyenSanXuatInfo)
            {
                if (mode == BUS.BUSControl.TransferMode.AddNew) Add((DTO.ChuyenSanXuatInfo)inf, ref sErr);
                else if (mode == BUS.BUSControl.TransferMode.Update) sErr = Update((ChuyenSanXuatInfo)inf);
                else sErr = InsertUpdate((ChuyenSanXuatInfo)inf);
            }
            return sErr;
        }

        public override DTO.DTOInfo[] Preview(XmlDocument xml, ref string sErr, string db = null)
        {
            DTO.DTOInfo[] lRes = null;
            XmlElement doc = xml.DocumentElement;
            lRes = new DTOInfo[doc.ChildNodes.Count];

            for (int i = 0; i < doc.ChildNodes.Count; i++)
            {
                if (sErr != "") return lRes;
                try
                {
                    XmlElement ele = doc.ChildNodes[i] as XmlElement;

                    DTO.ChuyenSanXuatInfo inf = new DTO.ChuyenSanXuatInfo();

                    inf.XuongId = db;
                    if (GetStringAttribute(ele, "LineId") != "") inf.LineId = GetInt32Attribute(ele, "LineId");
                    if (GetStringAttribute(ele, "LoaiBang") != "") inf.LoaiBang = GetStringAttribute(ele, "LoaiBang");
                    if (GetStringAttribute(ele, "TenChuyen") != "") inf.TenChuyen = GetStringAttribute(ele, "TenChuyen");
                    if (GetStringAttribute(ele, "KhachHang") != "") inf.KhachHang = GetStringAttribute(ele, "KhachHang");
                    if (GetStringAttribute(ele, "MaHang") != "") inf.MaHang = GetStringAttribute(ele, "MaHang");
                    if (GetStringAttribute(ele, "SoLuongMaHang") != "") inf.SoLuongMaHang = GetInt64Attribute(ele, "SoLuongMaHang");
                    if (GetStringAttribute(ele, "DonGia") != "") inf.DonGia = GetDoubleAttribute(ele, "DonGia");
                    if (GetStringAttribute(ele, "SoLaoDong") != "") inf.SoLaoDong = GetInt32Attribute(ele, "SoLaoDong");
                    if (GetStringAttribute(ele, "SoVang") != "") inf.SoVang = GetInt32Attribute(ele, "SoVang");
                    if (GetStringAttribute(ele, "TyLeDat") != "") inf.TyLeDat = GetDoubleAttribute(ele, "TyLeDat");
                    if (GetStringAttribute(ele, "SoGiaiDoan") != "") inf.SoGiaiDoan = GetInt32Attribute(ele, "SoGiaiDoan");
                    if (GetStringAttribute(ele, "DinhMuc1") != "") inf.DinhMuc1 = GetInt32Attribute(ele, "DinhMuc1");
                    if (GetStringAttribute(ele, "DinhMuc2") != "") inf.DinhMuc2 = GetInt32Attribute(ele, "DinhMuc2");
                    if (GetStringAttribute(ele, "DinhMuc3") != "") inf.DinhMuc3 = GetInt32Attribute(ele, "DinhMuc3");
                    if (GetStringAttribute(ele, "DinhMuc4") != "") inf.DinhMuc4 = GetInt32Attribute(ele, "DinhMuc4");
                    if (GetStringAttribute(ele, "DinhMuc5") != "") inf.DinhMuc5 = GetInt32Attribute(ele, "DinhMuc5");
                    if (GetStringAttribute(ele, "HoanTat1") != "") inf.HoanTat1 = GetInt32Attribute(ele, "HoanTat1");
                    if (GetStringAttribute(ele, "HoanTat2") != "") inf.HoanTat2 = GetInt32Attribute(ele, "HoanTat2");
                    if (GetStringAttribute(ele, "HoanTat3") != "") inf.HoanTat3 = GetInt32Attribute(ele, "HoanTat3");
                    if (GetStringAttribute(ele, "HoanTat4") != "") inf.HoanTat4 = GetInt32Attribute(ele, "HoanTat4");
                    if (GetStringAttribute(ele, "HoanTat5") != "") inf.HoanTat5 = GetInt32Attribute(ele, "HoanTat5");
                    if (GetStringAttribute(ele, "LuyKeSL1") != "") inf.LuyKeSL1 = GetInt32Attribute(ele, "LuyKeSL1");
                    if (GetStringAttribute(ele, "LuyKeSL2") != "") inf.LuyKeSL2 = GetInt32Attribute(ele, "LuyKeSL2");
                    if (GetStringAttribute(ele, "LuyKeSL3") != "") inf.LuyKeSL3 = GetInt32Attribute(ele, "LuyKeSL3");
                    if (GetStringAttribute(ele, "LuyKeSL4") != "") inf.LuyKeSL4 = GetInt32Attribute(ele, "LuyKeSL4");
                    if (GetStringAttribute(ele, "LuyKeSL5") != "") inf.LuyKeSL5 = GetInt32Attribute(ele, "LuyKeSL5");
                    if (GetStringAttribute(ele, "KeHoachSL1") != "") inf.KeHoachSL1 = GetInt32Attribute(ele, "KeHoachSL1");
                    if (GetStringAttribute(ele, "KeHoachSL2") != "") inf.KeHoachSL2 = GetInt32Attribute(ele, "KeHoachSL2");
                    if (GetStringAttribute(ele, "KeHoachSL3") != "") inf.KeHoachSL3 = GetInt32Attribute(ele, "KeHoachSL3");
                    if (GetStringAttribute(ele, "KeHoachSL4") != "") inf.KeHoachSL4 = GetInt32Attribute(ele, "KeHoachSL4");
                    if (GetStringAttribute(ele, "Comments") != "") inf.Comments = GetStringAttribute(ele, "Comments");
                    if (GetStringAttribute(ele, "WorkTimeOfDay") != "") inf.WorkTimeOfDay = GetInt32Attribute(ele, "WorkTimeOfDay");
                    if (GetStringAttribute(ele, "WorkTimeOfNow") != "") inf.WorkTimeOfNow = GetInt32Attribute(ele, "WorkTimeOfNow");
                    // details
                    //string details = GetStringAttribute(ele, "details");
                    //inf.details = JsonConvert.DeserializeObject<CsDataAccessGroupInfo.RoleDetail[]>(details);
                    lRes[i] = inf;
                }
                catch (Exception ex) { sErr = ex.Message; }
            }
            return lRes;
        }
        public bool validate(JObject o, bool pkOnly, ref string sErr)
        {
            bool inputErr = false;
            if (pkOnly)
            {
                if (o["LineId"] == null) { sErr += "Invalid LineId. Please check again"; inputErr = true; }
            }
            else
            {
                if (o["LineId"] == null) { sErr += "Invalid LineId. Please check again"; inputErr = true; }
                if (o["LoaiBang"] != null) { }//Check validate 
                if (o["TenChuyen"] != null) { }//Check validate 
                if (o["KhachHang"] != null) { }//Check validate 
                if (o["MaHang"] != null) { }//Check validate 
                if (o["SoLuongMaHang"] != null) { }//Check validate 
                if (o["DonGia"] != null) { }//Check validate 
                if (o["SoLaoDong"] != null) { }//Check validate 
                if (o["SoVang"] != null) { }//Check validate 
                if (o["TyLeDat"] != null) { }//Check validate 
                if (o["SoGiaiDoan"] != null) { }//Check validate 
                if (o["DinhMuc1"] != null) { }//Check validate 
                if (o["DinhMuc2"] != null) { }//Check validate 
                if (o["DinhMuc3"] != null) { }//Check validate 
                if (o["DinhMuc4"] != null) { }//Check validate 
                if (o["DinhMuc5"] != null) { }//Check validate 
                if (o["HoanTat1"] != null) { }//Check validate 
                if (o["HoanTat2"] != null) { }//Check validate 
                if (o["HoanTat3"] != null) { }//Check validate 
                if (o["HoanTat4"] != null) { }//Check validate 
                if (o["HoanTat5"] != null) { }//Check validate 
                if (o["LuyKeSL1"] != null) { }//Check validate 
                if (o["LuyKeSL2"] != null) { }//Check validate 
                if (o["LuyKeSL3"] != null) { }//Check validate 
                if (o["LuyKeSL4"] != null) { }//Check validate 
                if (o["LuyKeSL5"] != null) { }//Check validate 
                if (o["KeHoachSL1"] != null) { }//Check validate 
                if (o["KeHoachSL2"] != null) { }//Check validate 
                if (o["KeHoachSL3"] != null) { }//Check validate 
                if (o["KeHoachSL4"] != null) { }//Check validate 
                if (o["Comments"] != null) { }//Check validate 
                if (o["WorkTimeOfDay"] != null) { }//Check validate 
                if (o["WorkTimeOfNow"] != null) { }//Check validate 
            }
            return inputErr;
        }
        public override bool isField(string field)
        {
            return new DTO.ChuyenSanXuatInfo().isField(field);
        }
        #endregion Method

    }
}
